package viettravel.guiandevent.search;

import java.util.ArrayList;
import viettravel.DAO.ActivityDAO;
import viettravel.DAO.PlaceDAO;
import viettravel.DAO.SpecialtyDAO;
import viettravel.DAO.TouristFormDAO;
import viettravel.DTO.ViettravelDTO;
import viettravel.common.Constants;
import viettravel.guiandevent.R;
import viettravel.guiandevent.customeview.WidgetContainer;
import viettravel.guiandevent.place.NewPlaceExpadableChildObject;
import viettravel.guiandevent.place.NewPlaceExpadableHeaderObject;
import viettravel.guiandevent.place.NewPlaceExpandableAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.ExpandableListView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class PlaceSearch extends SherlockActivity {
	private NewPlaceExpandableAdapter mExpandableAdapter;
	private ExpandableListView mExpanableListView;
	private ArrayList<NewPlaceExpadableHeaderObject> mHeaderList;
	private ViettravelDTO mViettravelDTO;
	private ArrayList<NewPlaceExpadableChildObject> childListActivity;
	private ArrayList<NewPlaceExpadableChildObject> childListTourist;
	private ArrayList<NewPlaceExpadableChildObject> childListSpecialty;

	private EditText mEditTextPlaceName;
	// private IcsSpinner regionSpinner;

	private String mPlaceName;
	private ArrayList<Integer> mListActivity;
	private ArrayList<Integer> mListTouristForm;
	private ArrayList<Integer> mListSpecialty;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setTheme(Constants.THEME);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_search_place);
		/* mContext = this; */
		mViettravelDTO = ViettravelDTO.getInstance();
		ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayHomeAsUpEnabled(false);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setIcon(R.drawable.ic_back_arrow);
		actionBar.setTitle(R.string.searchplace_title);
		actionBar.setBackgroundDrawable(Constants.APP_DRAWABLE);

		WidgetContainer newPlaceInformationHeader = (WidgetContainer) findViewById(R.id.searchPlaceInformationHeader);
		newPlaceInformationHeader.setTitle(
				getString(R.string.searchplace_information_header),
				Gravity.CENTER_HORIZONTAL);
		mEditTextPlaceName = (EditText) findViewById(R.id.searchPlaceName);
		mEditTextPlaceName.setTypeface(Constants.TYPEFACE_NYALA);
		mHeaderList = new ArrayList<NewPlaceExpadableHeaderObject>();
		mExpanableListView = (ExpandableListView) findViewById(R.id.searchPlaceOptionList);
		updateExpandableList(mExpanableListView);
		expandAll();
	}

	// Expandable list
	public void updateExpandableList(ExpandableListView e) {
		childListActivity = new ArrayList<NewPlaceExpadableChildObject>();
		ArrayList<ActivityDAO> listActivity = mViettravelDTO
				.findAllActivities();
		for (ActivityDAO a : listActivity) {
			NewPlaceExpadableChildObject c = new NewPlaceExpadableChildObject(
					false, a);
			childListActivity.add(c);
		}
		mHeaderList.add(new NewPlaceExpadableHeaderObject(childListActivity,
				getString(R.string.newplace_header_activity)));

		childListTourist = new ArrayList<NewPlaceExpadableChildObject>();
		ArrayList<TouristFormDAO> listTourist = mViettravelDTO
				.findAllTourists();
		for (TouristFormDAO a : listTourist) {
			NewPlaceExpadableChildObject c = new NewPlaceExpadableChildObject(
					false, a);
			childListTourist.add(c);
		}
		mHeaderList.add(new NewPlaceExpadableHeaderObject(childListTourist,
				getString(R.string.newplace_header_tourist)));

		childListSpecialty = new ArrayList<NewPlaceExpadableChildObject>();
		ArrayList<SpecialtyDAO> listSpecialty = mViettravelDTO
				.findAllSpecialties();
		for (SpecialtyDAO a : listSpecialty) {
			NewPlaceExpadableChildObject c = new NewPlaceExpadableChildObject(
					false, a);
			childListSpecialty.add(c);
		}
		mHeaderList.add(new NewPlaceExpadableHeaderObject(childListSpecialty,
				getString(R.string.newplace_header_specialty)));

		mExpandableAdapter = new NewPlaceExpandableAdapter(this, mHeaderList);
		e.setAdapter(mExpandableAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu _menu) {
		_menu.add(Menu.NONE, R.id.searchplace_subitem_search, Menu.NONE,
				this.getString(R.string.searchplace_submenu_search))
				.setIcon(R.drawable.abs__ic_search_api_holo_light)
				.setShowAsAction(
						MenuItem.SHOW_AS_ACTION_IF_ROOM
								| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
		return super.onCreateOptionsMenu(_menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home: {
			finish();
			return true;
		}
		case R.id.searchplace_subitem_search: {
			Log.v(Constants.TAG, "SEARCH");
			getDataFromGUI();
			Constants.CURRENT_LIST_SEARCH = new ArrayList<PlaceDAO>();
			ArrayList<PlaceDAO> listTemp = new ArrayList<PlaceDAO>();
			listTemp = mViettravelDTO.findPlaceSearch(mPlaceName,
					mListSpecialty, mListActivity, mListTouristForm);
			if (mPlaceName != "") {
				for (PlaceDAO rowPlace : listTemp) {
					if (Constants
							.isContain(rowPlace.getPlaceName(), mPlaceName))
						Constants.CURRENT_LIST_SEARCH.add(rowPlace);
				}
			}
			Intent i = new Intent(PlaceSearch.this,
					ListPlaceSearchActivity.class);
			startActivity(i);
			return true;
		}
		default:
			return super.onMenuItemSelected(featureId, item);
		}
	}

	private void expandAll() {
		int count = mExpandableAdapter.getGroupCount();
		for (int i = 0; i < count; i++) {
			mExpanableListView.expandGroup(i);
		}
	}

	private void getDataFromGUI() {
		mPlaceName = mEditTextPlaceName.getText().toString();
		mListActivity = new ArrayList<Integer>();
		mListTouristForm = new ArrayList<Integer>();
		mListSpecialty = new ArrayList<Integer>();
		for (NewPlaceExpadableChildObject a : childListActivity) {
			if (a.getIsChecked())
				mListActivity.add(a.getCommonDao().getID());
		}
		for (NewPlaceExpadableChildObject a : childListTourist) {
			if (a.getIsChecked())
				mListTouristForm.add(a.getCommonDao().getID());
		}
		for (NewPlaceExpadableChildObject a : childListSpecialty) {
			if (a.getIsChecked())
				mListSpecialty.add(a.getCommonDao().getID());
		}
	}
}
